package cn.net.greenet.traffic.flow.group;

import cn.net.greenet.common.db_service.BaseDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 创建区域组和区域的对应关系
 *
 * @author liangdelin
 */
@WebServlet(name = "Create_Traffic_flow", urlPatterns = {"/traffic_flow/create_areas"})
public class Create_Traffic_flow extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String json = request.getParameter("json"); //获取UI的参数JSON
            JSONObject jo = JSONObject.fromObject(json);
            String group_name = jo.getString("group_name"); //区域组名称
            JSONArray jor = jo.getJSONArray("areas");  //区域组名称
            int group_id = 0;
            String areas = "";
            for (int i = 0; i < jor.size(); i++) {
                areas += jor.getString(i) + ",";
            }
            BaseDao baseDao = new BaseDao();
            int row = baseDao.executeUpdate("insert into traffic_flow_group_area(`group_name`,`areas`) values(?,?)", new Object[]{group_name, areas.substring(0, areas.length() - 1)});
            if (row != 0) {
                ResultSet rs = baseDao.executeSQL("select id from traffic_flow_group_area where group_name = ?", new Object[]{group_name});
                while (rs.next()) {
                    group_id = rs.getInt("id");
                }
                JSONObject joo = new JSONObject();
                joo.put("group_id", group_id);
                out.println(joo.toString());
                out.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
